<?php

namespace App\Jobs;

use App\Events\OrderComputedDistribution;
use App\Models\Order;
use App\Service\MiniProgramService;
use Flaravel\FLaravelSms;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;

class PaySuccessOrder implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $order;

    protected $prepayId;

    protected $template;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Order $order, $prepay_id, $template)
    {
        $this->order = $order;
        $this->prepayId = $prepay_id;
        $this->template = $template;
    }

    /**
     * 支付成功
     *
     * @return mixed
     */
    public function handle()
    {
        $data = [
            'keyword1' => $this->order->order_sn,
            'keyword2' => $this->order->total_amount,
            'keyword3' => $this->order->productName,
            'keyword4' => Carbon::parse($this->order->paid_at)->toDateTimeString(),
            'keyword5' => '您可以关注“葫芦家商城”公众号或者“发现-小程序”中找到',
        ];
        $user = $this->order->user()->first();
        $msg = app(MiniProgramService::class);
        $res = $msg->sendTemplateMsg(
            $user->wx_mini_openid,
            $this->template,
            $this->prepayId,
            '/pages/orderDetail/orderDetail?order_id=' . $this->order->id,
            $data
        );
        Log::info('支付成功，通知结果',$res);
        //发送短信
        if ($user->phone) {
            FLaravelSms::notice($user->phone,'addOrder',[]);
        }
    }
}
